﻿using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

public class XEditorTraceInspectorWindow : EditorWindow
{
    [MenuItem("Window/Trace")]
    static void OpenEditor()
    {
        XEditorTraceInspectorWindow build = GetWindow<XEditorTraceInspectorWindow>();
        build.titleContent = new GUIContent("Trace");
        build.Show();
    }

    enum TraceMode
    {
        Fight,
        Level,
        Action,
        Animation,
        Admin,
        Count
    }
    TraceMode traceMode = TraceMode.Admin;

    List<string> traceModeLabs = new List<string>();
    Dictionary<TraceMode, XEditorTraceTab> traceTabs = new Dictionary<TraceMode, XEditorTraceTab>();
    Texture2D refreshTexture;

    public XEditorTraceInspectorWindow()
    {
        for (TraceMode mode = TraceMode.Fight; mode < TraceMode.Count; mode++)
        {
            traceModeLabs.Add(mode.ToString());
        }
        
        traceTabs.Add(TraceMode.Admin, new XEditorAdminTab(this));
    }

    private Rect GetSubWindowArea()
    {
        Rect subPos = new Rect(0, 0, position.width, position.height);
        return subPos;
    }

    /// <summary>
    /// 
    /// </summary>
    private void OnEnable()
    {
        refreshTexture = EditorGUIUtility.FindTexture("Refresh");

        Rect subPos = GetSubWindowArea();
        foreach (KeyValuePair<TraceMode, XEditorTraceTab> v in traceTabs)
        {
            v.Value.OnEnable(subPos);
        }
    }

    /// <summary>
    /// 
    /// </summary>
    private void OnDisable()
    {
        foreach (KeyValuePair<TraceMode, XEditorTraceTab> v in traceTabs)
        {
            v.Value.OnDisable();
        }
    }

    /// <summary>
    /// 
    /// </summary>
    private void OnGUI()
    {
        Rect subPos = GetSubWindowArea();

        traceMode = (TraceMode)GUILayout.Toolbar((int)traceMode, traceModeLabs.ToArray());
        GUILayout.BeginHorizontal();
        GUILayout.FlexibleSpace();
        if (GUILayout.Button(refreshTexture, GUILayout.MaxWidth(30)))
        {
            foreach (KeyValuePair<TraceMode, XEditorTraceTab> tab in traceTabs)
            {
                tab.Value.OnReload();
            }
        }
        GUILayout.EndHorizontal();

        switch (traceMode)
        {
            case TraceMode.Admin:
                if (traceTabs.ContainsKey(TraceMode.Admin))
                    traceTabs[TraceMode.Admin].OnGUI(subPos);
                break;

            case TraceMode.Fight:
                if (traceTabs.ContainsKey(TraceMode.Fight))
                    traceTabs[TraceMode.Fight].OnGUI(subPos);
                break;

            case TraceMode.Action:
                if (traceTabs.ContainsKey(TraceMode.Action))
                    traceTabs[TraceMode.Action].OnGUI(subPos);
                break;

            case TraceMode.Animation:
                if (traceTabs.ContainsKey(TraceMode.Animation))
                    traceTabs[TraceMode.Animation].OnGUI(subPos);
                break;

            case TraceMode.Level:
                if (traceTabs.ContainsKey(TraceMode.Level))
                    traceTabs[TraceMode.Level].OnGUI(subPos);
                break;
        }
    }

    int fps = 0;
    private void Update()
    {
        fps++;
        if (fps >= 10)
        {
            Repaint();
            fps = 0;
        }
    }
}